package com.googlecode.moneyManager.spreadSheetReader.model;

import com.google.gdata.data.spreadsheet.CellEntry;
import com.googlecode.moneyManager.model.Currency;
import com.googlecode.moneyManager.reader.DataValueProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Author: Sergiy Doroshenko
 * Date: Jul 21, 2010
 * Time: 1:42:54 PM
 */
@Repository
public class DataValueProviderSpreadsheetGoogle implements DataValueProvider {

    private SpreadsheetGoogleDocument spreadsheetGoogleDocument;

    public SpreadsheetGoogleDocument getSpreadsheetGoogleDocument() {
        return spreadsheetGoogleDocument;
    }

    @Autowired
    public void setSpreadsheetGoogleDocument(SpreadsheetGoogleDocument spreadsheetGoogleDocument) {
        this.spreadsheetGoogleDocument = spreadsheetGoogleDocument;
    }

    public String getSumAmount(Currency currency) {
        switch (currency) {
            case USD:
                return getCellValue("H6").toString();
            case UAH:
                return getCellValue("H5").toString();
        }
        return null;
    }

    public String getPartAmount(Currency currency) {
        switch (currency) {
            case USD:
                return getCellValue("H1").toString();
            case EUR:
                return getCellValue("H2").toString();
            case UAH:
                return getCellValue("H3").toString();
        }
        return null;
    }

    public String getRate(Currency currency) {
        switch (currency) {
            case USD:
                return getCellValue("J1").toString();
            case EUR:
                return getCellValue("J2").toString();
        }
        return null;
    }

    private Number getCellValue(String cellName) {
        for (CellEntry cell : spreadsheetGoogleDocument.getStaticGarbageCellEntries()) {
            if (cell.getTitle().getPlainText().equals(cellName))
                return cell.getCell().getNumericValue();
        }
        return null;
    }
}
